package gui;

import javax.swing.JPanel;

import Entities.UserEntity;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
/**
 * 
 * @author Idan Mann
 * This GUI Class presents a simple Committee member access level switch mechanism between two users
 *
 */
public class SwitchCommitteeMemberPanel extends JPanel implements ChatIF
{
	private JTextField newCommitteeMembertextField;
	private AcademicFrame frame;
	private ManageEmployeesPanel previousPanel;
	private ArrayList<UserEntity> listOfCommitteeMembers;
	private UserEntity currentUser;
	private JComboBox comboBox;
	private JButton btnCancel;
	private JButton btnConfirm;
	private ArrayList<Object> switchCommitteeMembers;
	
	public SwitchCommitteeMemberPanel(AcademicFrame frame, ManageEmployeesPanel previousPanel, ArrayList<UserEntity> listOfCommitteeMembers, UserEntity currentUser) 
	{
		this.frame = frame;
		this.previousPanel = previousPanel;
		this.listOfCommitteeMembers = listOfCommitteeMembers;
		this.currentUser = currentUser;
		initialize();
		
		newCommitteeMembertextField.setText(currentUser.getFirstName() + " " + currentUser.getLastName());
		comboBox.addItem("--Choose a Committee Member--");
		for (int i =0; i<listOfCommitteeMembers.size() ; i++)
			comboBox.addItem(listOfCommitteeMembers.get(i).getFirstName() + " " + listOfCommitteeMembers.get(i).getLastName() );
		
		(new Dialog("You have chosen to replace one of the committee memebers, please confirm.")).setVisible(true);
		
	}
	private void initialize()
	{
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Switch Committee Member");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel.setBounds(177, 27, 213, 35);
		add(lblNewLabel);
		
		newCommitteeMembertextField = new JTextField();
		newCommitteeMembertextField.setEditable(false);
		newCommitteeMembertextField.setBounds(243, 82, 178, 20);
		add(newCommitteeMembertextField);
		newCommitteeMembertextField.setColumns(10);
		
		JLabel lblNewCommitteeMember = new JLabel("New Committee Member");
		lblNewCommitteeMember.setBounds(98, 85, 135, 14);
		add(lblNewCommitteeMember);
		
		comboBox = new JComboBox();
		comboBox.setBounds(239, 131, 182, 22);
		add(comboBox);
		
		JLabel lblReplaces = new JLabel("Replaces");
		lblReplaces.setBounds(98, 135, 46, 14);
		add(lblReplaces);
		
		btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				if (comboBox.getSelectedIndex()==0)
				{
					(new Dialog("Please select a committee member first.")).setVisible(true);
				}
				else
				{
					switchCommitteeMembers = new ArrayList<Object>();
					switchCommitteeMembers.add("switchCommitteeMembers");
					switchCommitteeMembers.add(listOfCommitteeMembers.get(comboBox.getSelectedIndex() - 1));
					switchCommitteeMembers.add(currentUser);
					AcademicFrame.myclient.sendMessageToServer(switchCommitteeMembers);
				}
			}
		});
		btnConfirm.setBounds(319, 200, 91, 23);
		add(btnConfirm);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				(new Dialog("No changes were saved.")).setVisible(true);
				frame.setContentPane(previousPanel);
				frame.setSize(650,500);
			}
		});
		btnCancel.setBounds(98, 200, 91, 23);
		add(btnCancel);
	}

	@Override
	public void display(Object message)
	{
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("CommitteeMemberSwitched")	)
		{
			if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
			{
				ArrayList<Object> setUserAccess = new ArrayList<Object>();
				setUserAccess.add("setUserAccess");
				setUserAccess.add(currentUser);
				AcademicFrame.myclient.sendMessageToServer(setUserAccess);
			}
			else
			{
				(new Dialog((String)(answer.get(1)))).setVisible(true);
			}
		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("userAccessWasSet")	)
			{

				if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
				{
					(new Dialog("User access level was updated.")).setVisible(true);
					frame.setContentPane(previousPanel);
					frame.setSize(650,500);
				}
				else
				{
					(new Dialog((String)(answer.get(1)))).setVisible(true);
					frame.setContentPane(previousPanel);
					frame.setSize(650,500);
				}
			}
		
		
	}

}
